package lz.controls.listClasses
{
	
	import lz.controls.ButtonLabelPlacement;
	import lz.controls.listClasses.ListData;
	import lz.controls.listClasses.ICellRenderer;
	import lz.controls.LabelButton;
	import lz.controls.core.UIComponent;
	import flash.events.Event;
	import flash.events.MouseEvent;

    //--------------------------------------
    //  Class description
    //--------------------------------------
	/**
	 * CellRenderer 类定义基于列表的组件的方法和属性，以用来处理和显示每一行的自定义单元格内容。 
	 * 自定义单元格可以包含文本、现有的组件（如 CheckBox）或您创建的任何类。 
	 * 使用该类的基于列表的组件包括 List、DataGrid、TileList 和 ComboBox 组件。
	 */
	public class CellRenderer extends LabelButton implements ICellRenderer 
	{
		
		/**
         *应用到单元格的列表属性，例如 index 和 selected 值。
		 */
		protected var _listData:ListData;


		/**
         * 组件关联的数据。
		 */
		protected var _data:Object;
        /**
         * 创建新的 CellRenderer 实例。
         */
		public function CellRenderer():void {
			super();
			toggle = true;
			focusEnabled = false;
		}
		
        /**
         *默认样式
         */
		private static var defaultStyles:Object = {upSkin:"CellRenderer_upSkin",downSkin:"CellRenderer_downSkin",overSkin:"CellRenderer_overSkin",
												  disabledSkin:"CellRenderer_disabledSkin",
												  selectedDisabledSkin:"CellRenderer_selectedDisabledSkin",
												  selectedUpSkin:"CellRenderer_selectedUpSkin",selectedDownSkin:"CellRenderer_selectedDownSkin",selectedOverSkin:"CellRenderer_selectedOverSkin",
												  textFormat:null,
												  disabledTextFormat:null,
												  embedFonts:null,
												  textPadding:5};
        /**
         * 检索当前组件的默认样式映射。
		 */
		public static function getStyleDefinition():Object { return defaultStyles; }
		/**
		 * 设置组件大小
		 */
		override public function setSize(width:Number,height:Number):void 
		{
			super.setSize(width, height);
		}
		/**
         * 获取组件数据
		 */
		public function get listData():ListData 
		{
			return _listData;
		}	
		/**
         * @private (setter)
		 */
		public function set listData(value:ListData):void
		{
			_listData = value;
			label = _listData.label;
			setStyle("icon", _listData.icon);
		}
		
		/**
		 * @copy listClasses.ICellRenderer#data
		 */
		public function get data():Object
		{
			return _data;
		}		
		/**
         * @private (setter)
		 */
		public function set data(value:Object):void
		{
			_data = value;
		}
		
		/**
         * @copyICellRenderer#selected
         *
         * @default false
		 */
		override public function get selected():Boolean 
		{
			return super.selected;
		}
		 
		/**
         * @private (setter)
		 */
		override public function set selected(value:Boolean):void 
		{
			super.selected = value;
		}
		
		/**
         * @private (protected)
		 */
		override protected function toggleSelected(event:MouseEvent):void 
		{
			// don't set selected or dispatch change event.
		}
		/**
         * @private (protected)
		 */
		override protected function drawLayout():void 
		{
			var textPadding:Number = Number(getStyleValue("textPadding"));
			var textFieldX:Number = 0;
			
			// Align icon
			if (icon != null)
			{
				icon.x = textPadding;
				icon.y = Math.round((height-icon.height)>>1);
				textFieldX = icon.width + textPadding;
			}
			
			// Align text
			if (label.length > 0) 
			{
				textField.visible = true;
				var textWidth:Number =  Math.max(0, width - textFieldX - textPadding*2);
				textField.width = textWidth;
				textField.height = textField.textHeight + 4;
				textField.x = textFieldX + textPadding
				textField.y = Math.round((height-textField.height)>>1);
			} else 
			{
				textField.visible = false;
			}
			
			// Size background
			background.width = width;
			background.height = height;
		}
	}
}

